package com.aizhixin.lab.docker.service;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.docker.domain.ContainerDomainV2;
import com.aizhixin.lab.docker.entity.ContainerToImages;
import com.aizhixin.lab.docker.repository.ContainerToImagesRepository;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Slf4j
@Service
@Transactional
public class ContainerToImagesService {
    @Autowired
    private ContainerToImagesRepository containerToImagesRepository;

    public void save(ContainerDomainV2 domainV2, AccountDTO accountDTO, String version){
        delete(domainV2,accountDTO.getId());
        ContainerToImages containerToImages = new ContainerToImages();
        containerToImages.setId(UUID.randomUUID().toString());
        containerToImages.setCourseId(domainV2.getCourseId());
        containerToImages.setChapterId(domainV2.getChapterId());
        containerToImages.setUserId(accountDTO.getId());
        containerToImages.setImagesName(accountDTO.getUserName());
        containerToImages.setImageVersion(version);
        containerToImagesRepository.save(containerToImages);
    }
    public void delete(ContainerDomainV2 domainV2,Long userId){
        containerToImagesRepository.deleteByCourseIdAndChapterIdAndUserId(domainV2.getCourseId(),domainV2.getChapterId(),userId);
    }
    public void save(ContainerToImages containerToImages){
        containerToImagesRepository.save(containerToImages);
    }
    public void save(List<ContainerToImages> containerToImages){
        containerToImagesRepository.save(containerToImages);
    }



}
